export interface TreeNode<T> {
  children?: TreeNode<T>[];
  [key: string]: T | TreeNode<T>[] | undefined | unknown;
}

/**
 *@author Buke
 * @description 指定key获取树中所有key的数组
 * @param tree  传入的树
 * @param key 指定获取的key
 * @returns keys 返回所有节点的key
 */
export const getTreeKeys = <T extends TreeNode<T>>(
  tree: T[],
  key: keyof T,
): T[keyof T][] => {
  const keys: T[keyof T][] = [];
  const getNodeKeys = (tree: T[]) => {
    tree.forEach((item) => {
      if (item.children && item.children.length > 0) {
        keys.push(item[key]);
        getNodeKeys(item?.children as T[]);
      }
    });
  };
  getNodeKeys(tree);
  return keys;
};
